 //
//  MeHeaderView.swift
//  DollarBuy
//
//  Created by 郑东喜 on 2016/11/8.
//  Copyright © 2016年 郑东喜. All rights reserved.
//

import UIKit


protocol MeHeaderDelegare {
    func jumpToLogin()
}

class MeHeaderView: UIView {

    var delegate : MeHeaderDelegare?
    
    var label = UILabel()
    
    
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.

    
    
    override func draw(_ rect: CGRect) {
        // Drawing code
        self.frame.size = CGSize(width: UIScreen.main.bounds.width, height: 150 )
        
        setLabelWithTag()
        setImgWithTag()
        logout()
    }
}

//表格透视图头事件
extension MeHeaderView {
    ///电话号码
    func setLabelWithTag() -> Void {

        label = viewWithTag(myHeadTelTag) as! UILabel
        label.isUserInteractionEnabled = true
        label.text = "点击登陆"
        
        let tapGes = UITapGestureRecognizer.init(target: self, action: #selector(MeHeaderView.clickLabel))
        label.addGestureRecognizer(tapGes)
    }
    
    ///头像
    func setImgWithTag() -> Void {
        
        let headImg = viewWithTag(myHeadImgTag) as! UIImageView
        headImg.isUserInteractionEnabled = true
        
        
        let tapGes = UITapGestureRecognizer.init(target: self, action: #selector(MeHeaderView.clickImg))
        headImg.addGestureRecognizer(tapGes)
    }
    
    ///退出
    func logout() -> Void {
        let logoutBtn = viewWithTag(myheadLogout) as! UIButton
        logoutBtn.addTarget(self, action: #selector(logoutSEL), for: .touchUpInside)
    }
    
}

 
// MARK:- 点击文字label进行登陆
extension MeHeaderView {
    func clickLabel() -> Void {
        print("单机表头视图标签进行登陆")
        delegate?.jumpToLogin()
        
        let uVC = UserVC()
        UIApplication.shared.keyWindow?.rootViewController?.navigationController?.pushViewController(uVC, animated: true)
    }
    
    func clickImg() -> Void {
        print("单机头像")
    }
}


extension MeHeaderView {
    func chnageNam() {
        print("\((#file as NSString).lastPathComponent):(\(#line))")
        label.text = "登陆成功"
    }
}

// MARK:- 退出事件
extension MeHeaderView {
    func logoutSEL() -> Void {
        if localSave.object(forKey: userToken) as? String != nil {
            let uToken = localSave.object(forKey: userToken) as! String
            
            let param = ["token" : uToken] as [String : String]
            print("\((#file as NSString).lastPathComponent):(\(#line))")
            
            print(param)
            
            let url = "\(logoutURL)\("?")\("token=")\(uToken)"
            
            
            NetTool.shareInstance.postWithPath(path: url, paras: nil, success: { (response) in
                print(response)

                let dic = response as! NSDictionary
                    
                let msg = dic["resultmsg"] as! String
                print(msg)
                
                
                
            }) { (error) in
                print("\((#file as NSString).lastPathComponent):(\(#line))")
                
                print(error.localizedDescription)
            }
            
           
        } else {
            print("token不存在")
        }

    }
}
